package cn.dlc.com.handler;

import cn.dlc.com.config.RsaPropertiesConfig;
import cn.dlc.com.pojo.Payload;
import cn.dlc.com.pojo.SysUser;
import cn.dlc.com.utils.JsonUtils;
import cn.dlc.com.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * 这个不会经过什么权限校验里面了，！！！
 * 不会经过 BasicAuthenticationFilter，直接进入LogoutFilter
 * 然后LogoutFilter调用这个地方的方法，
 */
@Component
public class SystemLogoutSuccessHandler implements LogoutSuccessHandler {


    @Autowired
    private RsaPropertiesConfig config;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {


        // TODO 需要实现登出逻辑
        // 登录之后在数据库里面写修改数据库里面的记录


        response.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = response.getWriter();

        Map<String,Object> map = new HashMap<>();
        map.put("code",HttpServletResponse.SC_OK);
        map.put("msg","退出成功！");
        writer.write(JsonUtils.toString(map));
        writer.flush();
        writer.close();
    }
}
